define(["jquery", "easy-admin", "treetable", "iconPickerFa"],
    function ($, ea) {

        var table = layui.table,
            treetable = layui.treetable,
            form = layui.form,
            iconPickerFa = layui.iconPickerFa;

        var init = {
            table_elem: '#currentTable',
            table_render_id: 'currentTableRenderId',
            index_url: '/system/menu',
            add_url: '/system/menu/create',
            delete_url: '/system/menu/destroy',
            edit_url: '/system/menu/edit',
            modify_url: '/system/menu/modify',
            config_url: '/system/menu/config',
        };

        return {
            index: function () {

                var renderTable = function () {

                    layer.load(2);
                    $cols = [[
                        {type: 'checkbox'},
                        {field: 'title', width: 250, title: '菜单名称', align: 'left'},
                        {field: 'icon', width: 80, title: '图标', templet: ea.table.icon},
                        {field: 'url', minWidth: 120, title: '菜单链接'},
                        {
                            field: 'is_home',
                            width: 80,
                            title: '类型',
                            templet: function (d) {
                                if (d.pid === 0) {
                                    return '<span class="layui-badge layui-bg-gray">模块</span>';
                                } else {
                                    return '<span class="layui-badge-rim">菜单</span>';
                                }
                            }
                        },
                        {field: 'sort', width: 80, title: '排序'},
                        {
                            field: 'status',
                            width: 80,
                            search: 'select',
                            title: '显示状态',
                            selectList: {0: '隐藏', 1: '显示'},
                            templet: ea.table.switch
                        },
                        {
                            width: 200,
                            title: '操作',
                            templet: ea.table.tool,
                            operat: [
                                [{
                                    text: '编辑',
                                    url: init.edit_url,
                                    method: 'open',
                                    auth: 'edit',
                                    class: 'layui-btn layui-btn-xs layui-btn-success',
                                    extend: 'data-full="true"',
                                }],
                                [{
                                    class: 'layui-btn layui-btn-danger layui-btn-xs',
                                    method: 'get',
                                    field: 'id',
                                    icon: '',
                                    text: '删除',
                                    title: '确定删除？',
                                    auth: 'delete',
                                    url: init.delete_url,
                                    extend: "data-callback='location.reload()'"
                                }],
                            ]
                        }
                    ]];


                    treetable.render({
                        treeColIndex: 1,
                        treeSpid: 0,
                        homdPid: 99999999,
                        treeIdName: 'id',
                        treePidName: 'pid',
                        url: ea.url(init.index_url),
                        elem: init.table_elem,
                        id: init.table_render_id,
                        toolbar: '#toolbar',
                        skin: 'line',
                        treeDefaultClose: true,
                        init: {modify_url: '/user/admin/modify',},
                        // @todo 不直接使用ea.table.render(); 进行表格初始化, 需要使用 ea.table.formatCols(); 方法格式化`cols`列数据
                        cols: ea.table.formatCols($cols, init),
                        done: function () {
                            layer.closeAll('loading');
                            ea.table.renderSwitch($cols, init, init.table_render_id, false);
                        }
                    });
                };

                renderTable();

                $('body').on('click', '[data-treetable-refresh]', function () {
                    renderTable();
                });

                $('body').on('click', '[data-treetable-delete]', function () {
                    var tableId = $(this).attr('data-treetable-delete'),
                        url = $(this).attr('data-url');
                    tableId = tableId || init.table_render_id;
                    url = url != undefined ? ea.url(url) : window.location.href;
                    var checkStatus = table.checkStatus(tableId),
                        data = checkStatus.data;
                    if (data.length <= 0) {
                        ea.msg.error('请勾选需要删除的数据');
                        return false;
                    }
                    var ids = [];
                    $.each(data, function (i, v) {
                        ids.push(v.id);
                    });
                    ea.msg.confirm('确定删除？', function () {
                        ea.request.post({
                            url: url,
                            data: {
                                id: ids
                            },
                        }, function (res) {
                            ea.msg.success(res.msg, function () {
                                renderTable();
                            });
                        });
                    });
                    return false;
                });

                ea.table.listenEdit(init, 'currentTable', init.table_render_id, true);

                ea.listen();
            },
            create: function () {
                iconPickerFa.render({
                    elem: '#icon',
                    url: PATH_CONFIG.iconLess,
                    limit: 12,
                    click: function (data) {
                        $('#icon').val('fa ' + data.icon);
                    },
                    success: function (d) {
                        console.log(d);
                    }
                });

                ea.listen(function (data) {
                    return data;
                }, function (res) {
                    ea.msg.success(res.msg, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.$('[data-treetable-refresh]').trigger("click");
                    });
                });
            },
            edit: function () {
                iconPickerFa.render({
                    elem: '#icon',
                    url: PATH_CONFIG.iconLess,
                    limit: 12,
                    click: function (data) {
                        $('#icon').val('fa ' + data.icon);
                    },
                    success: function (d) {
                        console.log(d);
                    }
                });


                ea.listen(function (data) {
                    return data;
                }, function (res) {
                    ea.msg.success(res.msg, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.$('[data-treetable-refresh]').trigger("click");
                    });
                });
            },
            config: function () {
                ea.listen();
            }
        };
    });
